
package loader.sandbox;

import lxl.net.AbstractContent;

import loader.Loader;
import loader.Main;

import jbxml.Component;
import jbxml.Processor;
import jbxml.Reflector;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.FileChannel;


/**
 * Files "jbx".
 * 
 * Jbxml is a dynamic XML binding to java beans.
 * 
 */
public class ContentJbxml
    extends AbstractContent
    implements jbxml.Component.Root,
               jbxml.Dictionary<String,Object>
{
    public final static String Fext = "jbx";


    private volatile String documentBase;

    private volatile Reflector reflect;

    private volatile Object tree;


    public ContentJbxml(URL source){
        super(source,false);
    }
    public ContentJbxml(String codebase, String path){
        super(codebase,path,false);
    }
    public ContentJbxml(String url){
        super(url,false);
    }


    public boolean hasTree(){
        return (null != this.tree);
    }
    public boolean hasNotTree(){
        return (null == this.tree);
    }
    public Object getTree(){
        return this.tree;
    }
    /*
     * component root
     */
    public void setDocumentBase(java.net.URL url){

        this.documentBase = url.toString();
    }
    public java.net.URL getInputURL(String path)
        throws java.net.MalformedURLException
    {
        return Loader.SourceUrl(this.documentBase, path);
    }
    public java.io.InputStream getInputStream(String path)
        throws java.io.IOException
    {
        AbstractContent content = new AbstractContent(this.documentBase,path);
        if (content.isNotLoaded())
            content.download();
        return content.getInputStream();
    }
    public Reflector getReflect(){
        return this.reflect;
    }
    public Component getParent(){
        return null;
    }
    public void startElement(Reflector bean){
        this.reflect = bean;
    }
    public void endElement(){
    }
    protected void downloaded(Main loader) throws IOException {

        File target = this.target();

        Processor proc = new Processor(this,this);

        InputStream in = this.getInputStream();
        try {
            this.tree = proc.readObject(in);
        }
        finally {
            in.close();
        }
    }

    /*
     * dictionary properties
     */
    public Object get(Object key){
        return null;
    }
    public Object put(String key, Object value){
        return null;
    }
    public Object remove(Object key){
        return null;
    }
    public boolean containsKey(Object key){
        return false;
    }
    public boolean isEmpty(){
        return true;
    }
    public java.util.Iterator<String> iterator(){
        throw new UnsupportedOperationException();
    }
}
